// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        isCustom: {
            default: true,
            notify() {
                //设置数值后更新一次
                this._updateCustom();
            }
        },
        isLayout: false,
        isExtractWidth: true,
        isExtractHeight: true
    },
    _updateCustom() {
        if (!this.isCustom) {
            this.isExtractWidth = cc.Canvas.instance.fitHeight;
            this.isExtractHeight = cc.Canvas.instance.fitWidth;
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        const ratioX = cc.view.getCanvasSize().width / cc.view.getVisibleSizeInPixel().width;
        const ratioY = cc.view.getCanvasSize().height / cc.view.getVisibleSizeInPixel().height;
        if (this.isLayout) {
            var laytou = this.getComponent(cc.Layout);
            if (laytou.type === cc.Layout.HORIZONTAL) {
                laytou.spacingX = laytou.spacingX * ratioX;
            } else {
                laytou.spacingX = laytou.spacingX * ratioY;
            }
        }
        if (!this.isCustom) {
            this.node.x = this.node.x * ratioX;
            this.node.y = this.node.y * ratioY;
        }else{
            if (this.isExtractWidth && this.isExtractHeight) {
                this.node.x = this.node.x * ratioX;
                this.node.y = this.node.y * ratioY;
            } else if (this.isExtractWidth && !this.isExtractHeight) {
                this.node.x = this.node.x * ratioX;
            } else if (!this.isExtractWidth && this.isExtractHeight) {
                this.node.y = this.node.y * ratioY;
            } else {
                this.node.x = this.node.x * ratioX;
                this.node.y = this.node.y * ratioY;
            }
        }
    },

    start() {
    },

    // update (dt) {},
});
